{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法一：递归\n",
    "\n",
    "# Definition for a binary tree node.\n",
    "# class TreeNode:\n",
    "#     def __init__(self, x):\n",
    "#         self.val = x\n",
    "#         self.left = None\n",
    "#         self.right = None\n",
    "\n",
    "class Solution:\n",
    "    def sumNumbers(self, root: TreeNode) -> int:\n",
    "        sum = 0\n",
    "        if(not root):\n",
    "            return 0\n",
    "        if(root.left):\n",
    "            root.left.val += root.val*10\n",
    "            sum += self.sumNumbers(root.left)\n",
    "        if(root.right):\n",
    "            root.right.val += root.val*10\n",
    "            sum += self.sumNumbers(root.right)\n",
    "        if(not root.left and not root.right):\n",
    "            return root.val\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二：辅助栈非递归\n",
    "\n",
    "# Definition for a binary tree node.\n",
    "# class TreeNode:\n",
    "#     def __init__(self, x):\n",
    "#         self.val = x\n",
    "#         self.left = None\n",
    "#         self.right = None\n",
    "\n",
    "class Solution:\n",
    "    def sumNumbers(self, root: TreeNode) -> int:\n",
    "        stack = []\n",
    "        p = root\n",
    "        sum = 0\n",
    "        while(p or stack):\n",
    "            if(p):\n",
    "                stack.append(p)\n",
    "                if(p.left):\n",
    "                    p.left.val += p.val*10\n",
    "                p = p.left\n",
    "            else:\n",
    "                p = stack.pop()\n",
    "                if(not p.left and not p.right):\n",
    "                    sum += p.val\n",
    "                if(p.right):\n",
    "                    p.right.val += p.val*10\n",
    "                p = p.right\n",
    "        return sum\n",
    "\n",
    "                "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
